package com.ruitu.ssmic.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.util.ScreenUtils;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.router_module.Router;
import com.ruitu.router_module.util.GlideImageLoader;
import com.ruitu.ssmic.R;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MainHomeFragment extends BaseFragment {
    private LinearLayout ll_menu_1, ll_menu_2;//圆形的菜单,下半部分菜单
    private LinearLayout ll_add_house;//添加房产
    private Banner banner;

    public static MainHomeFragment newInstance() {

        Bundle args = new Bundle();

        MainHomeFragment fragment = new MainHomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_main_home, container, false);

        ll_menu_1 = v.findViewById(R.id.ll_menu_1);
        ll_menu_2 = v.findViewById(R.id.ll_menu_2);
        ll_add_house = v.findViewById(R.id.ll_add_house);
        banner = v.findViewById(R.id.banner);

        int margin_l_r = ScreenUtils.getScreenWidth() - SizeUtils.dp2px(100);
        int margin_l_r_2 = ScreenUtils.getScreenWidth() - SizeUtils.dp2px(50);
        RelativeLayout.LayoutParams p1 = (RelativeLayout.LayoutParams) ll_menu_1.getLayoutParams();
        p1.width = margin_l_r;
        p1.height = p1.width;
        ll_menu_1.setLayoutParams(p1);

        RelativeLayout.LayoutParams p2 = (RelativeLayout.LayoutParams) ll_menu_2.getLayoutParams();
        p2.width = margin_l_r_2;
        ll_menu_2.setLayoutParams(p2);

        initBanner();
        setListeners();

        return v;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == ll_add_house) {//添加房产
            try {
                Router.startActivity(getActivity(), "com.ruitu.ssmic.Main1Activity");
            } catch (ClassNotFoundException e) {
                ToastUtils.showShortSafe("打开页面失败");
            }
        }
    }

    private void setListeners() {
        ll_add_house.setOnClickListener(this);
    }

    private void initBanner() {
        List<String> urlList = new ArrayList<>();//图片地址集合
        urlList.add("https://zhsq.lianxinyi.com.cn:8445/images/ad/6125581500460874516_1501745272851.jpg");
        urlList.add("https://zhsq.lianxinyi.com.cn:8445/images/ad/4551787472585048964_1524050276649.jpg");
        urlList.add("https://zhsq.lianxinyi.com.cn:8445/images/ad/7711195208466938569_1524050152884.jpg");
        try {
            banner.setImages(urlList);//设置图片集合
            banner.setImageLoader(new GlideImageLoader());//设置图片加载器
            banner.setDelayTime(6000);//设置轮播时间
            banner.setIndicatorGravity(BannerConfig.RIGHT);
            banner.setBannerAnimation(Transformer.Accordion);
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int pos) {
                    ToastUtils.showShortSafe("点击Banner....." + pos);
                }
            });
            banner.start();//banner设置方法全部调用完毕时最后调用
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
